package com.mzplayer.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.mzplayer.utils.RS;
import com.mzplayer.utils.Util;
import com.mzplayer.videoview.base.StandardParent;

/**
 * @Package: com.mzplayer.widget
 * @ClassName: FullContainer
 * @Description: java类作用描述
 * @Author: 麦子
 * @QQ: 3332174009
 * @CreateDate: 2021/7/27 22:24
 */
public class FullContainer extends Dialog {
    private final StandardParent standardParent;
    private final Window mainWindow;
    private int statusBarColor;

    public FullContainer(StandardParent standardParent) {
        super(standardParent.getContext(), RS.style.mz_full_dialog);
        setCancelable(false);
        @SuppressLint("InlinedApi") final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION //隐藏导航栏
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION //视图延伸至导航栏区域，导航栏上浮于视图之上
                | View.SYSTEM_UI_FLAG_FULLSCREEN //状态栏隐藏
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //视图延伸至状态栏区域，状态栏上浮于视图之上
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; //保持稳定 使View不会因为System UI的变化而重新layout
        final View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                decorView.setSystemUiVisibility(uiOptions);
            }
        });
        this.standardParent = standardParent;
        this.mainWindow = Util.scanForActivity(standardParent.getContext()).getWindow();
    }

    @Override
    public void show() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            statusBarColor = mainWindow.getStatusBarColor();
            mainWindow.setStatusBarColor(standardParent.getBackgroundColor());
        }
        Util.clearParent(standardParent);
        setContentView(standardParent);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
        super.show();
    }

    @Override
    public void dismiss() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mainWindow.setStatusBarColor(statusBarColor);
        }
        Util.clearParent(standardParent);
        super.dismiss();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK||event.getKeyCode()==KeyEvent.KEYCODE_ESCAPE) {
            if(isShowing()&&event.getAction()!=KeyEvent.ACTION_MULTIPLE) {
                standardParent.onFullKeyBackEvent(event.getAction());
            }
            return true;
        }
        return Util.scanForActivity(getContext()).dispatchKeyEvent(event);
    }

}